$(function() {
	load();	
	//查询今日固化数量
    selectNum();
    //查询所有正在固化的台车列表
    selectList();
    //查询固化区的台车总数
    selectTc();
    //查询固化区的组件总数
    selectZc();
    //查询初始设定时间
    selectTime();
    //查询固化区域
    selectPosition();
    
    //调用定时器
    setIntervalDemo()
	
})

    function keyDown(e){  
        //IE内核浏览器  
         if (navigator.appName == 'Microsoft Internet Explorer'){  
               var keycode = event.keyCode;  
               var realkey = String.fromCharCode(event.keyCode);  
         }else {//非IE内核浏览器  
               var keycode = e.which;  
               var realkey = String.fromCharCode(e.which);  
         }  

       //监听Ctrl键  
       if(keycode==13){ 
    	   if($("#icode").val() != null && $("#icode").val() != "" && $("#icode").val() != undefined) {
    		   var icode =  $("#icode").val();
    			$("#icode").val(icode+",");
    		   return;
    	   }
    	   
    	   var cremark=$.trim($("#tc1").val());
    	   var cremark5=$.trim($("#cremark5").text());
    	   
    	   if(cremark!= null && cremark != "" && cremark != undefined) {
    		   $.ajax({
   			  	async:false, 
   		    	type:'post',
   		    	url:'/solid/edit2.do',
   		    	data: {"cremark":cremark,"cremark5":cremark5},
   		    	success:function(data){
   		    		if(data > 0){
   		    			toastr.success(cremark+"台车固化开始！");
   		    			selectList();
   		    			selectNum();
   		    			selectTc();
   		    			selectZc();
   		    		    selectPosition();
   		    		    
   		    		    notifyAgv(1);
   		    		    
   		    			flg = false;
   		    		}else if(data == -1){
   		    			toastr.warning("对不起，无此数据！");
   		    			notifyAgv(2);
   		    			flg = false;
   		    		}else if(data == -2){
   		    			toastr.warning("台车已经固化开始，请不要重复扫码！");
   		    			notifyAgv(1);
   		    			flg = false;
   		    		}else{
   		    			toastr.warning("操作失败！");
   		    			notifyAgv(2);
   		    			flg = false;
   		    		}
   		    	}
   		    })
   		    $("#tc1").val("");
       		$("#tc1").select();  
    	  }
       }  
    }  
    //按键事件
    document.onkeydown=keyDown;	
	
    //统计今日固化数量
    function selectNum() {
    	$.ajax({
	    	type:'post',
	    	url:'/solid/selectActualNum.do',
	    	success:function(data){
	    		$("#actualNum").html(data);
	    	}
	    });
	}	
    
    //查询所有正在固化的台车列表
    function selectList() {
    	$.ajax({
	    	type:'post',
	    	dataType: 'json',
	    	url:'/solid/selectList.do',
	    	success:function(data){
	    		if(data[0] != null && data[0] != "" && data[0] != undefined ) {
	    			
	    			for(var i = 1; i < data.length+1; i++) {
	    				//台车
			    		$("#c"+i).html(data[i-1].cremark);
			    		//组件数量
			    		$("#z"+i).html(data[i-1].cremark4);
			    		//开始时间
			    		$("#t"+i).html(data[i-1].starttime);
	    			}
	    		}else {
	    			$("#c1").html("");
		    		//组件数量
		    		$("#z1").html("");
		    		//开始时间
		    		$("#t1").html("");
	    		}
	    	}
	    });
	}
    
    //查询台车总数
    function selectTc() {
    	$.ajax({
	    	type:'post',
	    	dataType: 'json',
	    	url:'/solid/selectTcount.do',
	    	success:function(data){
	    		$("#tc").html(data);
	    	}
	    });
	}
    
    //查询组件总数
    function selectZc() {
    	$.ajax({
	    	type:'post',
	    	dataType: 'json',
	    	url:'/solid/selectZcount.do',
	    	success:function(data){
	    		$("#zc").html(data);
	    	}
	    });
    }
    
    var index;
	function pei() {
		$("#searchName").val("");
		$("#serch").click();
		
		index = layer.open({
			type: 1 					//Page层类型
	  		,area: ['750px', '600px'] 	//宽  高
	  		,title: '选择配件'
	  		,shade: 0.6 				//遮罩透明度
	  		,maxmin: true 				//允许全屏最小化
	  		,anim: 1 					//0-6的动画形式，-1不开启
	  		,content: $("#peiDivID")
	  		,end: function(){
	  			$("#peiDivID").hide();
	  		} 
		});
	}
    
    //查询固化设定时间
    function selectTime() {
    	$.ajax({
    		type:'post',
    		dataType: 'json',
    		url:'/solid/selectTime.do',
    		success:function(data){
    			$("#cremark5").html(data);
    		}
    	});
    }
    
    //查询固化区域
    function selectPosition() {
    	$.ajax({
    		type:'post',
    		dataType: 'json',
    		url:'/solid/selectPosition.do',
    		success:function(data){
    			
    			for(var i = 1; i < 6; i++) {
    				$("#A"+i).html('<img alt="" src="/img/agv2.png"><br/>');
    				$("#B"+i).html('<img alt="" src="/img/agv2.png"><br/>');
    				$("#C"+i).html('<img alt="" src="/img/agv2.png"><br/>');
    				$("#D"+i).html('<img alt="" src="/img/agv2.png"><br/>');
    				$("#E"+i).html('<img alt="" src="/img/agv2.png"><br/>');
    				$("#F"+i).html('<img alt="" src="/img/agv2.png"><br/>');
    				$("#G"+i).html('<img alt="" src="/img/agv2.png"><br/>');
    			}
    			
				if(data[0] != null && data[0] != "" && data[0] != undefined ) {
	    			
	    			for(var i = 0; i < data.length; i++) {
	    				var position = data[i].position;
	    				var tc = data[i].tc;
	    				var remark = data[i].remark;
	    				if("no" == remark) {
	    					$("#"+position).html('<img alt="" src="/img/agv3.png"><br/>'+tc);
	    				}else {
	    					$("#"+position).html('<img alt="" src="/img/agv1.png"><br/>'+tc);
	    				}
	    			}
	    		}
    		}
    	});
	}
    
    //通知agv扫码结果
    function notifyAgv(flag) {
    	$.ajax({
    		type:'post',
    		dataType: 'json',
    		data: {"flag":flag},
    		url:'/solidNotifyAgv.do',
    		success:function(data){
    			//toastr.success("已通知小车");
    		}
    	});
	}
    
    //定时器
    function setIntervalDemo(){
        setTimeoutName = setInterval(function(){
        	 selectPosition();
        	 selectTc();
	    	 selectZc();
        	 $.ajax({
   			  	async:false, 
   		    	type:'post',
   		    	data: {"flag":2},
   		    	url:'/arriveReade.do',
   		    	success:function(data){
   		    		if(data == 1) {
   		    			var cremark=$.trim($("#tc1").val());
	   		         	var cremark5=$.trim($("#cremark5").text());
	   		         	//var cremark5=$.trim($("#cremark5").text());
	   		      	   if(cremark!= null && cremark != "" && cremark != undefined) {
	   		      		  $.ajax({
	   		      			  	async:false, 
	   		      		    	type:'post',
	   		      		    	url:'/solid/edit2.do',
	   		      		    	data: {"cremark":cremark,"cremark5":cremark5},
	   		      		    	success:function(data){
	   		      		    		if(data > 0){
	   		      		    			toastr.success(cremark+"台车固化开始！");
	   		      		    			selectList();
	   		      		    			selectNum();
	   		      		    			selectTc();
	   		      		    			selectZc();
	   		      		    		    selectPosition();
	   		      		    		    
	   		      		    		    notifyAgv(1);
	   		      		    		    
	   		      		    			flg = false;
	   		      		    		}else if(data == -1){
	   		      		    			toastr.warning("对不起，无此数据！");
	   		      		    			notifyAgv(3);
	   		      		    			flg = false;
	   		      		    		}else if(data == -2){
	   		      		    			toastr.warning("台车已经固化开始，请不要重复扫码！");
	   		      		    			notifyAgv(3);
	   		      		    			flg = false;
	   		      		    		}else{
	   		      		    			toastr.warning("操作失败！");
	   		      		    			notifyAgv(2);
	   		      		    			flg = false;
	   		      		    		}
	   		      		    	}
	   		      		    })
	   		      		    $("#tc1").val("");
	   		          		$("#tc1").select();
	   		      		   
	   		      	   }else {
	   		      		   notifyAgv(2);
	   		      	   }
   		    		}
   		    	}
   		    });
        },1000);
    }

    
